/*
 * CDamageResultRenderer.java
 *
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 *
 * This file is part of JRogueDPS.
 *
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

package dpsresults;

import dpsstats.CDamageStats;
import dpssystem.CAttackChance.HitType;
import dpssystem.CUtil;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Mani
 */
public class CDamageResultRenderer extends JLabel
                           implements TableCellRenderer {
    Border unselectedBorder = null;
    Border selectedBorder = null;
    boolean isBordered = true;

    public CDamageResultRenderer() {
        this.isBordered = true;
        setOpaque(true); //MUST do this for background to show up.
    }

    public Component getTableCellRendererComponent(
                            JTable table, Object p_object,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
        CDamageStats l_damageStats = (CDamageStats) p_object;
        if (l_damageStats == null)
        {

            this.setText("");
            this.setBackground(Color.WHITE);
            this.setIcon(null);
            return this;
        }

        setBackground(Color.WHITE);

        switch(column)
        {
            case 0: this.setText(l_damageStats.getName()); break;
            case 1: this.setText("" + CUtil.trunc2(l_damageStats.getTotalDamage())); break;
            case 2: this.setText(this.getHitTypeValue(l_damageStats, HitType.NORMAL)); break;
            case 3: this.setText(this.getHitTypeValue(l_damageStats, HitType.CRIT)); break;
            case 4: this.setText(this.getHitTypeValue(l_damageStats, HitType.MISS)); break;
            case 5: this.setText(this.getHitTypeValue(l_damageStats, HitType.GLANCING)); break;
            case 6: this.setText(this.getHitTypeValue(l_damageStats, HitType.DODGE)); break;            
            default : this.setText("");
        }

        return this;
    }

    private String getHitTypeValue(CDamageStats p_damageStats, HitType p_hitType)
    {
        int l_count = p_damageStats.getStats(p_hitType).getCount();
        if (l_count == 0)
        {
            return "0";
        }

        int l_totalCount = p_damageStats.getTotalCount();
        return l_count + " (" + CUtil.trunc2(100.0 * ((double) l_count / (double) l_totalCount)) + "%) " +
                CUtil.trunc2(p_damageStats.getStats(p_hitType).getDamage() / l_count);
    }
}
